{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "#导入numpy模块,设置别称为np\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.18.1\n",
      "blas_mkl_info:\n",
      "    libraries = ['mkl_rt']\n",
      "    library_dirs = ['C:/Users/PINE/anaconda3\\\\Library\\\\lib']\n",
      "    define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]\n",
      "    include_dirs = ['C:\\\\Program Files (x86)\\\\IntelSWTools\\\\compilers_and_libraries_2019.0.117\\\\windows\\\\mkl', 'C:\\\\Program Files (x86)\\\\IntelSWTools\\\\compilers_and_libraries_2019.0.117\\\\windows\\\\mkl\\\\include', 'C:\\\\Program Files (x86)\\\\IntelSWTools\\\\compilers_and_libraries_2019.0.117\\\\windows\\\\mkl\\\\lib', 'C:/Users/PINE/anaconda3\\\\Library\\\\include']\n",
      "blas_opt_info:\n",
      "    libraries = ['mkl_rt']\n",
      "    library_dirs = ['C:/Users/PINE/anaconda3\\\\Library\\\\lib']\n",
      "    define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]\n",
      "    include_dirs = ['C:\\\\Program Files (x86)\\\\IntelSWTools\\\\compilers_and_libraries_2019.0.117\\\\windows\\\\mkl', 'C:\\\\Program Files (x86)\\\\IntelSWTools\\\\compilers_and_libraries_2019.0.117\\\\windows\\\\mkl\\\\include', 'C:\\\\Program Files (x86)\\\\IntelSWTools\\\\compilers_and_libraries_2019.0.117\\\\windows\\\\mkl\\\\lib', 'C:/Users/PINE/anaconda3\\\\Library\\\\include']\n",
      "lapack_mkl_info:\n",
      "    libraries = ['mkl_rt']\n",
      "    library_dirs = ['C:/Users/PINE/anaconda3\\\\Library\\\\lib']\n",
      "    define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]\n",
      "    include_dirs = ['C:\\\\Program Files (x86)\\\\IntelSWTools\\\\compilers_and_libraries_2019.0.117\\\\windows\\\\mkl', 'C:\\\\Program Files (x86)\\\\IntelSWTools\\\\compilers_and_libraries_2019.0.117\\\\windows\\\\mkl\\\\include', 'C:\\\\Program Files (x86)\\\\IntelSWTools\\\\compilers_and_libraries_2019.0.117\\\\windows\\\\mkl\\\\lib', 'C:/Users/PINE/anaconda3\\\\Library\\\\include']\n",
      "lapack_opt_info:\n",
      "    libraries = ['mkl_rt']\n",
      "    library_dirs = ['C:/Users/PINE/anaconda3\\\\Library\\\\lib']\n",
      "    define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]\n",
      "    include_dirs = ['C:\\\\Program Files (x86)\\\\IntelSWTools\\\\compilers_and_libraries_2019.0.117\\\\windows\\\\mkl', 'C:\\\\Program Files (x86)\\\\IntelSWTools\\\\compilers_and_libraries_2019.0.117\\\\windows\\\\mkl\\\\include', 'C:\\\\Program Files (x86)\\\\IntelSWTools\\\\compilers_and_libraries_2019.0.117\\\\windows\\\\mkl\\\\lib', 'C:/Users/PINE/anaconda3\\\\Library\\\\include']\n"
     ]
    }
   ],
   "source": [
    "#显示numpy的版本号和配置文件\n",
    "print(np.__version__)\n",
    "np.show_config()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1.37960283e-306 1.11260959e-306 4.67288259e-307 1.95818994e-306\n",
      " 6.23036978e-307 6.23058028e-307 8.45595292e-307 9.34612506e-307\n",
      " 3.22650139e-307 6.89800737e-307]\n",
      "[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n",
      "[[5. 5. 5.]\n",
      " [5. 5. 5.]]\n"
     ]
    }
   ],
   "source": [
    "#创建一个大小为10的空向量\n",
    "print(np.empty(10))\n",
    "print(np.zeros(10))\n",
    "print(np.full((2,3),5.0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "24\n",
      "12\n"
     ]
    }
   ],
   "source": [
    "#查看数组占用内存大小\n",
    "sample4_1 = np.empty((3, 2), np.uint32)\n",
    "sample4_2 = np.empty((3, 2), np.float16)\n",
    "print(sample4_1.itemsize * sample4_1.size)\n",
    "print(sample4_2.itemsize * sample4_2.size)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "add(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj])\n",
      "\n",
      "Add arguments element-wise.\n",
      "\n",
      "Parameters\n",
      "----------\n",
      "x1, x2 : array_like\n",
      "    The arrays to be added. If ``x1.shape != x2.shape``, they must be broadcastable to a common shape (which becomes the shape of the output).\n",
      "out : ndarray, None, or tuple of ndarray and None, optional\n",
      "    A location into which the result is stored. If provided, it must have\n",
      "    a shape that the inputs broadcast to. If not provided or None,\n",
      "    a freshly-allocated array is returned. A tuple (possible only as a\n",
      "    keyword argument) must have length equal to the number of outputs.\n",
      "where : array_like, optional\n",
      "    This condition is broadcast over the input. At locations where the\n",
      "    condition is True, the `out` array will be set to the ufunc result.\n",
      "    Elsewhere, the `out` array will retain its original value.\n",
      "    Note that if an uninitialized `out` array is created via the default\n",
      "    ``out=None``, locations within it where the condition is False will\n",
      "    remain uninitialized.\n",
      "**kwargs\n",
      "    For other keyword-only arguments, see the\n",
      "    :ref:`ufunc docs <ufuncs.kwargs>`.\n",
      "\n",
      "Returns\n",
      "-------\n",
      "add : ndarray or scalar\n",
      "    The sum of `x1` and `x2`, element-wise.\n",
      "    This is a scalar if both `x1` and `x2` are scalars.\n",
      "\n",
      "Notes\n",
      "-----\n",
      "Equivalent to `x1` + `x2` in terms of array broadcasting.\n",
      "\n",
      "Examples\n",
      "--------\n",
      ">>> np.add(1.0, 4.0)\n",
      "5.0\n",
      ">>> x1 = np.arange(9.0).reshape((3, 3))\n",
      ">>> x2 = np.arange(3.0)\n",
      ">>> np.add(x1, x2)\n",
      "array([[  0.,   2.,   4.],\n",
      "       [  3.,   5.,   7.],\n",
      "       [  6.,   8.,  10.]])\n"
     ]
    }
   ],
   "source": [
    "#查看numpy中add函数的用法\n",
    "np.info(np.add)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]\n"
     ]
    }
   ],
   "source": [
    "#创建一个大小为10的空向量，将第5个值设为1\n",
    "sample = np.zeros(10)\n",
    "sample[4] = 1\n",
    "print(sample)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33\n",
      " 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49]\n"
     ]
    }
   ],
   "source": [
    "#用10到49的序列构建一个向量\n",
    "sample2 = np.arange(10, 50) # arange 同样不包含stop的值。\n",
    "print(sample2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4 5 6 7 8 9]\n",
      "[9 8 7 6 5 4 3 2 1 0]\n"
     ]
    }
   ],
   "source": [
    "#将一个数组变换倒序\n",
    "print(np.arange(10))\n",
    "print(np.arange(10)[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 1 2]\n",
      " [3 4 5]\n",
      " [6 7 8]]\n"
     ]
    }
   ],
   "source": [
    "#用0-8这9个数构造一个3x3大小的矩阵\n",
    "sample3 = np.arange(9).reshape((3, -1))\n",
    "print(sample3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(array([0, 1, 4], dtype=int64),)\n"
     ]
    }
   ],
   "source": [
    "#从数组[1,2,0,0,4,0]中找出非0元素的下标\n",
    "print(np.nonzero([1, 2, 0, 0, 4, 0]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1. 0. 0.]\n",
      " [0. 1. 0.]\n",
      " [0. 0. 1.]]\n",
      "[[1. 0. 0.]\n",
      " [0. 1. 0.]\n",
      " [0. 0. 1.]]\n"
     ]
    }
   ],
   "source": [
    "#创建3x3的对角矩阵\n",
    "\n",
    "print(np.identity(3))\n",
    "\n",
    "print(np.eye(3,3,0))  # eye可以创建NxM的矩阵，也可以控制对角线的位置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[0.89343823 0.32866427 0.14367809]\n",
      "  [0.4576561  0.13281947 0.87497438]\n",
      "  [0.17777368 0.41809178 0.56061302]]\n",
      "\n",
      " [[0.00764032 0.39788807 0.64211063]\n",
      "  [0.426141   0.63178044 0.95646796]\n",
      "  [0.47108175 0.08827817 0.06345879]]\n",
      "\n",
      " [[0.67986201 0.00945488 0.44791883]\n",
      "  [0.79900482 0.22599891 0.95762266]\n",
      "  [0.07361208 0.95735223 0.90599407]]]\n"
     ]
    }
   ],
   "source": [
    "#用随机数创建一个3x3x3的矩阵\n",
    "print(np.random.random((3, 3, 3)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.53421081 0.69444652 0.05212813 0.50102607 0.00930602 0.15593416\n",
      "  0.0347548  0.26397544 0.13611667 0.71240961]\n",
      " [0.17203588 0.58073375 0.43845114 0.29862227 0.18542463 0.84704649\n",
      "  0.40688851 0.80773655 0.3996958  0.45610451]\n",
      " [0.4501723  0.43253991 0.88393354 0.37650209 0.46749212 0.73019019\n",
      "  0.04228736 0.43099421 0.99764622 0.08325011]\n",
      " [0.9503079  0.2595255  0.1417701  0.72797155 0.88955251 0.96345537\n",
      "  0.12002761 0.38671752 0.98257118 0.10488007]\n",
      " [0.91278717 0.26552275 0.86401054 0.90954256 0.6600275  0.98935835\n",
      "  0.65642676 0.57660853 0.04952786 0.68483317]\n",
      " [0.92831212 0.40718152 0.30041971 0.28962856 0.2435336  0.60854323\n",
      "  0.7390699  0.07281614 0.03523652 0.72176973]\n",
      " [0.98866178 0.71130547 0.74524465 0.14774454 0.86086897 0.59408033\n",
      "  0.29612475 0.6608398  0.07442354 0.65343567]\n",
      " [0.6671244  0.45561221 0.76484823 0.94652356 0.08052481 0.03577822\n",
      "  0.66510183 0.23769668 0.50125417 0.96516662]\n",
      " [0.58976701 0.16239801 0.65654409 0.00375765 0.25614795 0.3830783\n",
      "  0.24218197 0.26977563 0.38452035 0.77486783]\n",
      " [0.92734035 0.54438261 0.7482568  0.23980897 0.82588001 0.61750767\n",
      "  0.17642693 0.5284676  0.27424815 0.0244873 ]]\n",
      "0.9976462204731353 0.0037576526893688955\n"
     ]
    }
   ],
   "source": [
    "#创建一个10x10的随机数矩阵，并找到最大值和最小值\n",
    "sample13 = np.random.random((10, 10))\n",
    "print(sample13)\n",
    "print(sample13.max(), np.min(sample13))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0. 0. 0. 0. 0. 0.]\n",
      " [0. 1. 1. 1. 1. 0.]\n",
      " [0. 1. 1. 1. 1. 0.]\n",
      " [0. 1. 1. 1. 1. 0.]\n",
      " [0. 1. 1. 1. 1. 0.]\n",
      " [0. 0. 0. 0. 0. 0.]]\n"
     ]
    }
   ],
   "source": [
    "#扩展给定数组的边界\n",
    "sample16 = np.ones((4,4))\n",
    "print(np.pad(sample16, 1, mode='constant', constant_values=0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 0 0 0 0]\n",
      " [1 0 0 0 0]\n",
      " [0 2 0 0 0]\n",
      " [0 0 3 0 0]\n",
      " [0 0 0 4 0]]\n"
     ]
    }
   ],
   "source": [
    "#用1，2，3，4做为对角线的下移一行，来创建5x5的矩阵\n",
    "print(np.diag([1, 2, 3, 4], -1))# hint diag函数的第二个参数指定对角线的位置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 1 0 1 0 1 0 1]\n",
      " [1 0 1 0 1 0 1 0]\n",
      " [0 1 0 1 0 1 0 1]\n",
      " [1 0 1 0 1 0 1 0]\n",
      " [0 1 0 1 0 1 0 1]\n",
      " [1 0 1 0 1 0 1 0]\n",
      " [0 1 0 1 0 1 0 1]\n",
      " [1 0 1 0 1 0 1 0]]\n"
     ]
    }
   ],
   "source": [
    "#使用tile函数创建一个棋盘\n",
    "print(np.tile([[0, 1], [1, 0]], (4, 4)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[3.7812367  9.46601822 0.19833402 5.38788243 6.46568029]\n",
      " [4.1311241  9.85671392 3.52046001 7.77851583 4.47694704]\n",
      " [3.35853384 6.49436128 8.75262903 4.69124123 4.23851055]\n",
      " [0.30954009 5.83621627 5.01256472 5.81058498 7.2957637 ]\n",
      " [2.62968178 6.52390083 0.3884027  3.4025421  5.37324036]]\n",
      "[[-0.48232804  1.75417736 -1.89191277  0.14975814  0.57378512]\n",
      " [-0.34467543  1.90788477 -0.58492275  1.09028057 -0.20862185]\n",
      " [-0.6486277   0.58506879  1.47351595 -0.12431428 -0.30242747]\n",
      " [-1.8481621   0.32614153  0.00210074  0.31605768  0.90035634]\n",
      " [-0.93537251  0.59669023 -1.817136   -0.63131398  0.14399766]]\n"
     ]
    }
   ],
   "source": [
    "#归一化一个5x5的随机矩阵\n",
    "sample22 = np.random.random((5,5))*10\n",
    "print(sample22)\n",
    "print((sample22 - sample22.mean()) / sample22.std()) # std 计算均方差"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[22 14]\n",
      " [72 53]\n",
      " [60 42]\n",
      " [74 52]\n",
      " [52 35]]\n",
      "[[22 14]\n",
      " [72 53]\n",
      " [60 42]\n",
      " [74 52]\n",
      " [52 35]]\n"
     ]
    }
   ],
   "source": [
    "#计算5x3和3x2矩阵的内积（点乘）\n",
    "sample24_1 = np.random.randint(0, 9, (5, 3))\n",
    "sample24_2= np.random.randint(0, 9, (3, 2))\n",
    "print(np.dot(sample24_1,sample24_2))\n",
    "print(sample24_1 @ sample24_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 2  3 -4 -5 -6 -7  8  9 10 11]\n"
     ]
    }
   ],
   "source": [
    "#反转一维数组中大于3小于8的所有元素\n",
    "sample25 = np.arange(2, 12)\n",
    "sample25[(sample25 >3) & (sample25 <8)] *= -1 # &表示and\n",
    "print(sample25)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2]\n"
     ]
    }
   ],
   "source": [
    "#查找两个数组的交集\n",
    "print(np.intersect1d([1, 2, 3], [4, 2, 1]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 1 2 3 4]\n",
      " [0 1 2 3 4]\n",
      " [0 1 2 3 4]\n",
      " [0 1 2 3 4]\n",
      " [0 1 2 3 4]]\n"
     ]
    }
   ],
   "source": [
    "#创建一个5x5每行为0到4的矩阵\n",
    "print(np.tile(np.arange(5), (5, 1)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 1 1 2 4 4 7 7 8]\n"
     ]
    }
   ],
   "source": [
    "#创建一个大小为10的数组并排序\n",
    "sample40 = np.random.randint(0, 9, 10)\n",
    "sample40.sort()\n",
    "print(sample40)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "#比较两个随机数组是否相等\n",
    "A = np.random.randint(0,2,5)\n",
    "B = np.random.randint(0,2,5)\n",
    "\n",
    "equal = np.allclose(A,B) #在1e-05的误差范围内比较\n",
    "print(equal)\n",
    "\n",
    "equal = np.array_equal(A,B) # 比较两个数组是否相等\n",
    "print(equal)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
